# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Salli Moustafa (salli.moustafa@huawei.com)
#!/bin/bash

if [ "x${DATASET}" == "x" ]; then
    echo "[ERROR] DATASET is not set"
    exit -1
fi

if [ "x${PROCESS_TYPE}" == "x" ]; then
    PROCESS_TYPE=training
fi

if [[ "${PROCESS_TYPE}" != "training" && "${PROCESS_TYPE}" != "inference" ]]; then
    echo "[ERROR] PROCESS_TYPE must be either training or inference"
    exit -1
fi

INFERENCE_FILES=inference_files-${DATASET}.tar.gz
INFERENCE_DATA_DIR=inference/data/${DATASET}
MODEL_FILE_DIR=inference/model/pb

if [ "${PROCESS_TYPE}" == "training" ]; then
    # prepare the archive containing the model file and input data for inference
    
    cd data
    
    mkdir -p ${INFERENCE_DATA_DIR}
    mkdir -p ${MODEL_FILE_DIR}

    cp ../data/features.npy ../data/adjacency.npy ../data/mask.npy ../data/labels.npy ${INFERENCE_DATA_DIR}
    cp ../results/constant_graph_${DATASET}.pb ${MODEL_FILE_DIR}

    tar cvfz ${INFERENCE_FILES} ${INFERENCE_DATA_DIR} ${MODEL_FILE_DIR}

    cd -
elif [ "${PROCESS_TYPE}" == "inference" ]; then
    # decompress the archive containing inference required files (model and input data)
    
    if [ ! -f "data/${INFERENCE_FILES}" ]; then
        echo "[ERROR] File \"data/${INFERENCE_FILES}\" not found"
        echo "[ERROR] ${INFERENCE_FILES} is generated by the training process. Copy it from the training platform."
        exit -1
    fi
    
    tar xvf data/${INFERENCE_FILES}
fi
